﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Web.Configuration;
using System.Web;

using ESF.General;

#endregion


namespace ESF.Web
{
	/// <summary>
	/// Description of WebConfig.
	/// </summary>
	public class WebConfigReader
	{
		#region Constantes
		
		private const int DEFAULTSESSIONTIMEOUTMINUTES = 90;
		
		#endregion
		
		
		#region Funciones
	
		public static int GetSessionTimeoutMinutes ()
		{
			return GetSessionTimeoutMinutes (DEFAULTSESSIONTIMEOUTMINUTES);
		}
		
		
		public static int GetSessionTimeoutMinutes (int pTimeoutMinutesDefault)
		{
			SessionStateSection sessionSection;
			
			
			try
			{
				sessionSection = (SessionStateSection) WebConfigurationManager.OpenWebConfiguration("~").GetSection("system.web/sessionState");	
						
				return (sessionSection == null ? pTimeoutMinutesDefault : Convert.ToInt32 (sessionSection.Timeout.TotalMinutes));
			}
			catch (Exception)
			{
				// Se omite y se da un valorp por defecto pues es un problema del web de desarrollo de vstudio
				return pTimeoutMinutesDefault;
			}
		}
		
		
		public static string GetCustomTimeoutURL ()
		{
			string urlTimeout;
			
		
			urlTimeout = ReadConfig.GetAppSetting ("RelativeURLSessionTimeout", string.Empty);
		
			if (string.IsNullOrEmpty (urlTimeout))
			{
				urlTimeout = ReadConfig.GetAppSetting ("AbsoluteURLSessionTimeout", "/");
			}
			else
			{
				if (!HttpContext.Current.Request.ApplicationPath.EndsWith ("/") && !urlTimeout.StartsWith ("/"))
				{
					urlTimeout = "/" + urlTimeout;
				}
			
				urlTimeout = string.Format ("{0}://{1}{2}{3}",
					HttpContext.Current.Request.Url.Scheme,
					HttpContext.Current.Request.Url.Authority,
					HttpContext.Current.Request.ApplicationPath,
					urlTimeout);
			}
			
			return urlTimeout;
		}
		
		#endregion
	}
}
